﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Net.TheBoredCoders.SoundBoard
{
    /// <summary>
    /// Class for handling the Data-Items from the Sound-Directory (Samples and Sub-Dirs)
    /// </summary>
    public class SampleData
    {
        private DataType dataType;
        private string name;
        private string details;
        private string path;
        private List<SampleData> childElements;
        private System.Drawing.Image image;

        /// <summary>
        /// Initializes a new instance of the <see cref="SampleData"/> class.
        /// </summary>
        public SampleData()
        {
            dataType = SoundBoard.DataType.undefined;
            name = string.Empty;
            details = string.Empty;
            path = string.Empty;
            childElements = new List<SampleData>();
            image = null;
        }

        /// <summary>
        /// Gets or sets the type of the data.
        /// </summary>
        /// <value>
        /// The type of the data.
        /// </value>
        public DataType DataType
        {
            get { return dataType; }
            set { dataType = value; }
        }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>
        /// The name.
        /// </value>
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        /// <summary>
        /// Gets or sets the details.
        /// </summary>
        /// <value>
        /// The details.
        /// </value>
        public string Details
        {
            get { return details; }
            set { details = value; }
        }

        /// <summary>
        /// Gets or sets the path.
        /// </summary>
        /// <value>
        /// The path.
        /// </value>
        public string Path
        {
            get { return path; }
            set { path = value; }
        }

        /// <summary>
        /// Gets or sets the child elements.
        /// </summary>
        /// <value>
        /// The child elements.
        /// </value>
        public List<SampleData> ChildElements
        {
            get { return childElements; }
            set { childElements = value; }
        }

        /// <summary>
        /// Gets or sets the image.
        /// </summary>
        /// <value>
        /// The image.
        /// </value>
        public System.Drawing.Image Image
        {
            get { return image; }
            set { image = value; }
        }

        /// <summary>
        /// Gets the tool tip.
        /// </summary>
        public string ToolTip
        {
            get {
                if (details.IndexOf('\n') > 0) // there is a line-break in the data - cut there and remove possible control-chars
                {
                    return details.Substring(0, details.IndexOf('\n')).Replace("\n", "").Replace("\r", "");
                }
                else // just return the full details or the name - there is no line-break
                {
                    if (details.Trim() == string.Empty) // on empty details return the name
                    {
                        return name;
                    }
                    else // or the details-line
                    {
                        return details;
                    }
                }
            }
        }

        /// <summary>
        /// Gets the child items of a specific type.
        /// </summary>
        /// <param name="dataType">Type of the data.</param>
        /// <returns></returns>
        public List<SampleData> GetChildItems(DataType dataType)
        {
            // create tmp-list
            List<SampleData> resultList = new List<SampleData>();
            // check all childern
            foreach (SampleData item in this.childElements)
            {
                // if type matches
                if (item.dataType == dataType)
                {
                    // add to result
                    resultList.Add(item);
                }

            }
            // return result
            return resultList;
        }

    }
}
